/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.WindowConstants;
import javax.swing.filechooser.FileNameExtensionFilter;
import model.Heightmap;
import model.ManipuladorArquivo;
import model.SuavizarHeightmap;
import model.estrutura.MalhaRegular;

/**
 *
 * @author Thiago
 */
public class TelaHeightmap extends JFrame implements ActionListener {

    Heightmap h;
    DrawHeightmap d;
    MalhaRegular m;
    JFileChooser fileChooser;
    Activity a;
    ManipuladorArquivo ManipuladorArquivo;

    //Objetos do menu
    JMenuBar menuBar;
    JMenu opcoes;
    JMenuItem suavizar;
    JMenuItem gerarRegular;
    JMenuItem arquivo_salvar;

    public TelaHeightmap(Heightmap h, MalhaRegular m, Activity a) {

        this.h = h;
        this.m = m;
        this.a = a;
        this.setLayout(new BorderLayout());
        this.setTitle("CgMalhaCar3D");
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        this.setSize(h.getTamanho() + 25, h.getTamanho() + 65);

        this.setVisible(true);

        initComponest();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == gerarRegular) {
            gerarRegular();
        }

        if (e.getSource() == suavizar) {
            SuavizarHeightmap suaviza = new SuavizarHeightmap(h);
            suaviza.suavizar();
            this.d.setParametros(h);
            this.d.repaint();
        }

        if (e.getSource() == arquivo_salvar) {
            this.fileChooser = new JFileChooser();
            this.fileChooser.setDialogTitle("Salvar malha como");
            this.fileChooser.setFileFilter(new FileNameExtensionFilter("cgmh", "cgmh"));
            this.fileChooser.setAcceptAllFileFilterUsed(false);
            this.fileChooser.setMultiSelectionEnabled(false);
            if (this.fileChooser.showSaveDialog(this) == this.fileChooser.APPROVE_OPTION) {
                try {
                    this.ManipuladorArquivo = new ManipuladorArquivo(this.h);
                    this.ManipuladorArquivo.saveHeightMap(this.fileChooser.getSelectedFile() + ".cgmh");
                    System.out.println("Salvou o arquivo...");
                } catch (Exception ex) {
                    Logger.getLogger(Activity.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    public void initComponest() {
        // menu bar
        this.menuBar = new JMenuBar();

        // menu
        this.opcoes = new JMenu("Opções");

        // itens menu
        this.suavizar = new JMenuItem("Suavizar");
        this.arquivo_salvar = new JMenuItem("Salvar");
        this.gerarRegular = new JMenuItem("Gerar Malha Regular");

        // add actions
        this.arquivo_salvar.addActionListener(this);
        this.suavizar.addActionListener(this);
        this.gerarRegular.addActionListener(this);

        // add no menu
        this.opcoes.add(this.arquivo_salvar);
        this.opcoes.add(this.suavizar);
        this.opcoes.add(this.gerarRegular);

        // add no menu bar
        this.menuBar.add(this.opcoes);

        // add menu bar no jframe
        this.setJMenuBar(menuBar);

        // add jpanel
        this.d = new DrawHeightmap();
        this.d.setParametros(this.h);
        this.add(d, BorderLayout.CENTER);
        this.d.repaint();
    }

    private void gerarRegular() {
        this.m = new MalhaRegular(h.getMapaMatriz());
        if (this.m == null) {
            System.out.println("Fuu..");
        }
        a.setVisao(false);
    }
}
